package com.emm.yixun.mobile.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.emm.yixun.mobile.R;
import com.emm.yixun.mobile.model.GetProjectBusinessList.HomePageList;

import java.util.ArrayList;

public class MainGridAdapter extends BaseAdapter {

    Context context;
    ArrayList<HomePageList> list;

    public MainGridAdapter(Context context, ArrayList<HomePageList> list) {
        this.context = context;
        this.list = list;
    }

    public void SetDate(ArrayList<HomePageList> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list == null ? null : list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolderMain v = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.main_listview_item, null);
            v = new ViewHolderMain();
            v.count = (TextView) convertView.findViewById(R.id.count);
            v.image_icon = (ImageView) convertView.findViewById(R.id.image_icon);
            v.name = (TextView) convertView.findViewById(R.id.name);
            convertView.setTag(v);
        } else {
            v = (ViewHolderMain) convertView.getTag();
        }
        String name = list.get(position).getHomePageName();
        String count = list.get(position).getHomePageNoReadNum();
        if ("逾期未签约".equals(name)) {
            v.count.setBackgroundResource(R.drawable.main_drop1);
            if (null == count || "0".equals(count)) {
                v.count.setVisibility(View.GONE);
            } else {
                v.count.setVisibility(View.VISIBLE);
            }
            v.image_icon.setImageResource(R.drawable.hom_ico1);
        } else if ("逾期未付款".equals(name)) {
            v.count.setBackgroundResource(R.drawable.main_drop1);
            if (null == count || "0".equals(count)) {
                v.count.setVisibility(View.GONE);
            } else {
                v.count.setVisibility(View.VISIBLE);
            }
            v.image_icon.setImageResource(R.drawable.hom_ico2);
        } else if ("待处理客户".equals(name)) {
            v.count.setBackgroundResource(R.drawable.main_drop1);
            if (null == count || "0".equals(count)) {
                v.count.setVisibility(View.GONE);
            } else {
                v.count.setVisibility(View.VISIBLE);
            }
            v.image_icon.setImageResource(R.drawable.hom_ico3);
        } else if ("公告通知".equals(name) || "公告".equals(name)) {
            v.count.setBackgroundResource(R.drawable.main_drop1);
            if (null == count || "0".equals(count)) {
                v.count.setVisibility(View.GONE);
            } else {
                v.count.setVisibility(View.VISIBLE);
            }
            v.image_icon.setImageResource(R.drawable.hom_ico4);
        } else if ("审核列表".equals(name) || "审核申请".equals(name)) {
            v.count.setBackgroundResource(R.drawable.main_drop1);
            if (null == count || "0".equals(count)) {
                v.count.setVisibility(View.GONE);
            } else {
                v.count.setVisibility(View.VISIBLE);
            }
            v.image_icon.setImageResource(R.drawable.hom_ico6);
        } else if ("提醒".equals(name)) {
            v.count.setBackgroundResource(R.drawable.main_drop1);
            if (null == count || "0".equals(count)) {
                v.count.setVisibility(View.GONE);
            } else {
                v.count.setVisibility(View.VISIBLE);
            }
            v.image_icon.setImageResource(R.drawable.hom_ico5);
        } else if ("下次跟进提醒".equals(name)) {
            v.count.setBackgroundResource(R.drawable.main_drop1);
            if (null == count || "0".equals(count)) {
                v.count.setVisibility(View.GONE);
            } else {
                v.count.setVisibility(View.VISIBLE);
            }
            v.image_icon.setImageResource(R.drawable.hom_ico7_720);
        } else if ("未完善信息客户".equals(name)) {
            v.count.setBackgroundResource(R.drawable.main_drop1);
            if (null == count || "0".equals(count)) {
                v.count.setVisibility(View.GONE);
            } else {
                v.count.setVisibility(View.VISIBLE);
            }
            v.image_icon.setImageResource(R.drawable.infor);
        } else if ("签约回款提醒".equals(name)) {
            v.count.setBackgroundResource(R.drawable.main_drop1);
            if (null == count || "0".equals(count)) {
                v.count.setVisibility(View.GONE);
            } else {
                v.count.setVisibility(View.VISIBLE);
            }
            v.image_icon.setImageResource(R.drawable.ic_money_back);
        } else if ("认购".equals(name)) {
            v.count.setBackgroundResource(R.drawable.main_drop1);
            if (null == count || "0".equals(count)) {
                v.count.setVisibility(View.GONE);
            } else {
                v.count.setVisibility(View.VISIBLE);
            }
            v.image_icon.setImageResource(R.drawable.ic_subscribe);
        } else if ("签约".equals(name)) {
            v.count.setBackgroundResource(R.drawable.main_drop1);
            if (null == count || "0".equals(count)) {
                v.count.setVisibility(View.GONE);
            } else {
                v.count.setVisibility(View.VISIBLE);
            }
            v.image_icon.setImageResource(R.drawable.ic_sign);
        } else {
            v.count.setVisibility(View.GONE);
        }

        try {
            v.count.setText(Integer.valueOf(count) >= 99 ? "99+" : count);
        } catch (Exception e) {
            v.count.setText("0");
        }
        v.name.setText(name);
        return convertView;
    }

    public static class ViewHolderMain {
        ImageView image_icon;
        TextView name;
        TextView count;
    }

}
